package com.zany.frame.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.login.service.LoginService;
import com.zany.system.vo.UserInfoVO;
import com.zany.login.vo.UserResourceVO;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.action.SessionInfo;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JsonTools;

/**
 * 页面 左菜单
 * @author chengY
 * @version 1.0
 */
public class MenuAction extends AbstractActionSupport {

	private static final long serialVersionUID = 7432975235145206588L;
	private static Log logger = LogFactory.getLog(MenuAction.class);
	private LoginService loginService;
	private String resourceCode;//功能代码
	private List menuList = new ArrayList();//功能列表
	private List<UserResourceVO> oneMenulist = new ArrayList<UserResourceVO>();
	private List<UserResourceVO> morelist = new ArrayList<UserResourceVO>();
	private UserResourceVO userResourceVO = new UserResourceVO();
	private StringBuffer menu = new StringBuffer("");
	
	public void oneMenu() throws Exception{
		if(logger.isDebugEnabled()){logger.info("MenuAction.leftMenu()");}
		HttpServletResponse response = ServletActionContext.getResponse();
		UserInfoVO userInfo = new UserInfoVO();
		userInfo.setRoleCode(SessionInfo.getRoleCode(request));
		if(SessionInfo.getRoleCode(request).equals("0")){
			oneMenulist = loginService.getTopMenuAdmin();
		}else{
			oneMenulist = loginService.getTopMenu(userInfo);
		}
		for(UserResourceVO vo:oneMenulist){
			Map menuMAp = new HashMap();
			menuMAp.put("MENUID",vo.getResourceCode());
			menuMAp.put("MENUNAME",vo.getResourceName());
			menuList.add(menuMAp);
		}
		AjaxTools.exAjax(JsonTools.lstMapToJson(menuList), response);
	}
	
	public void getMoreMenu() throws Exception{
		if(logger.isDebugEnabled()){logger.info("MenuAction.getMoreMune()");}
		HttpServletResponse response = ServletActionContext.getResponse();
		String topCode = request.getParameter("node");
		userResourceVO.setTopCode(topCode);
		userResourceVO.setRoleCode(SessionInfo.getRoleCode(request));
		if(SessionInfo.getRoleCode(request).equals("0")){
			morelist = loginService.getMoreMenuAdmin(userResourceVO);
		}else{
			morelist = loginService.getMoreMenu(userResourceVO);
		}
		menu.append("[");
		for(UserResourceVO vo:morelist){
			menu.append("{id:'"+vo.getResourceCode()+"',text:'"+vo.getResourceName()+"',");
			menu.append("location:'");
			menu.append(vo.getUrl());
			menu.append("',leaf:true,iconCls: 'task-folder'},");
		}
		String menuStr = menu.toString().substring(0,menu.toString().length()-1) + "]";
		AjaxTools.exAjax(menuStr, response);
	}
	
	public LoginService getLoginService() {
		return loginService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	public String getResourceCode() {
		return resourceCode;
	}
	public void setResourceCode(String resourceCode) {
		this.resourceCode = resourceCode;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		
	}

}
